// SysIconUtils.cpp

#include "StdAfx.h"

#include "SysIconUtils.h"

#include "Common/Lang/LangUtils.h"


//TODO: AM - this is very bad
#include "../../7zip/UI/FileManager/resource.h"


namespace Common
{
namespace FileSystem
{

int GetIconIndexForCSIDL(int csidl)
{
	LPITEMIDLIST pidl = 0;
	SHGetSpecialFolderLocation(NULL, csidl, &pidl);
	if (pidl)
	{
		SHFILEINFO shellInfo;
		SHGetFileInfo(LPCTSTR(pidl), FILE_ATTRIBUTE_NORMAL,
					  &shellInfo, sizeof(shellInfo),
					  SHGFI_PIDL | SHGFI_SYSICONINDEX);
		IMalloc*  pMalloc;
		SHGetMalloc(&pMalloc);
		if(pMalloc)
		{
			pMalloc->Free(pidl);
			pMalloc->Release();
		}
		return shellInfo.iIcon;
	}
	return 0;
}

DWORD_PTR GetRealIconIndex(LPCTSTR path, DWORD attrib, int &iconIndex)
{
	SHFILEINFO shellInfo;
	DWORD_PTR res = ::SHGetFileInfo(path, FILE_ATTRIBUTE_NORMAL | attrib, &shellInfo,
									sizeof(shellInfo), SHGFI_USEFILEATTRIBUTES | SHGFI_SYSICONINDEX);
	iconIndex = shellInfo.iIcon;
	return res;
}

DWORD_PTR GetRealIconIndex(const CString &fileName, DWORD attrib,
						   int &iconIndex, CString &typeName)
{
	{
		SHFILEINFO shellInfo;
		shellInfo.szTypeName[0] = 0;
		DWORD_PTR res = ::SHGetFileInfo(fileName, FILE_ATTRIBUTE_NORMAL | attrib, &shellInfo,
										sizeof(shellInfo), SHGFI_USEFILEATTRIBUTES | SHGFI_SYSICONINDEX | SHGFI_TYPENAME);
		typeName = shellInfo.szTypeName;
		iconIndex = shellInfo.iIcon;
		return res;
	}
}

size_t CExtToIconMap::GetIconIndex(DWORD attrib, const CString &fileName, CString &typeName)
{
	int dotPos = fileName.ReverseFind(_T('.'));
	if ((attrib & FILE_ATTRIBUTE_DIRECTORY) != 0 || dotPos < 0)
	{
		CAttribIconPair pair;
		pair.Attrib = attrib;
		ArrayAttribIconPair::const_iterator it = _attribMap.find(pair);
		if (it != _attribMap.end())
		{
			typeName = it->TypeName;
			return it->IconIndex;
		}
		GetRealIconIndex(
#ifdef UNDER_CE
			_T("\\")
#endif
			_T("__File__")
			, attrib, pair.IconIndex, pair.TypeName);
		_attribMap.insert(pair);
		typeName = pair.TypeName;
		return pair.IconIndex;
	}

	CExtIconPair pair;
	pair.Ext = fileName.Mid(dotPos + 1);
	ArrayExtIconPair::const_iterator it = _extMap.find(pair);
	if (it != _extMap.end())
	{
		typeName = it->TypeName;
		return it->IconIndex;
	}
	GetRealIconIndex(fileName.Mid(dotPos), attrib, pair.IconIndex, pair.TypeName);
	_extMap.insert(pair);
	typeName = pair.TypeName;
	return pair.IconIndex;
}

size_t CExtToIconMap::GetIconIndex(DWORD attrib, const CString &fileName)
{
	CString typeName;
	return GetIconIndex(attrib, fileName, typeName);
}


CString RootFolder_GetName_Computer(int &iconIndex)
{
	iconIndex = FileSystem::GetIconIndexForCSIDL(CSIDL_DRIVES);
	return Lang::LangString(IDS_COMPUTER, 0x03020300);
}

CString RootFolder_GetName_Network(int &iconIndex)
{
	iconIndex = FileSystem::GetIconIndexForCSIDL(CSIDL_NETWORK);
	return Lang::LangString(IDS_NETWORK, 0x03020301);
}

CString RootFolder_GetName_Documents(int &iconIndex)
{
	iconIndex = FileSystem::GetIconIndexForCSIDL(CSIDL_PERSONAL);
	return Lang::LangString(IDS_DOCUMENTS, 0x03020302);;
}



}
}